from flask import Flask, render_template, request
import copy

app = Flask(__name__)

name_list = ["张嘉锡", "赵健磊", "郭泰瑞", "秦晓川", "卢旺程", "李翔", "顾鼎"]
scores_data = {name: ["\\" if i == j else str(10) for j in range(7)] for i, name in enumerate(name_list)}

print(scores_data)


def restring(s):
    length_s = len(s)
    if length_s >= 7:
        return s[:7]
    left_spaces = (7 - length_s) // 2
    right_spaces = 7 - length_s - left_spaces
    return "-" * left_spaces + s + "-" * right_spaces


@app.route('/')
def index():
    global name_list, scores_data

    table_data = []
    for row in range(8):
        current_row = []
        if row == 0:
            current_row.append("被评分人|评分人")
            for name in name_list:
                current_row.append(restring(name))
        else:
            for col in range(0, 8):
                if col == 0:
                    current_row.append(name_list[row - 1])
                else:
                    if col - 1 < len(name_list) and row - 1 < len(scores_data[name_list[0]]):
                        score = scores_data[name_list[col - 1]][row - 1]
                        current_row.append(str(score))
                    else:
                        current_row.append("10")  # 如果索引越界，仍按默认值10处理

        table_data.append(current_row)

    return render_template('index.html', data=table_data)


@app.route('/update_scores', methods=['POST'])
def update_scores():
    global scores_data
    try:
        data = request.get_json()
        username = data.get('username')
        scores = data.get('scores')

        print(data)

        if username and scores and len(scores) == 7:
            updated_scores_data = copy.deepcopy(scores_data)
            for index in range(7):
                    updated_scores_data[username][index] = str(scores[index])
            scores_data = updated_scores_data
            print(scores_data)
            return 'success', 200
        return 'invalid data', 400
    except:
        return 'error', 500


def runfls():
    app.run(debug=True)


if __name__ == '__main__':
    runfls()